//
//  CDGuideViewController.m
//  CollectToDesktop
//
//  Created by 王启颖 on 2020/10/19.
//  Copyright © 2020 godbe. All rights reserved.
//

#import "CDGuideViewController.h"
#import "CDGuideCell.h"
#import "CDHomeViewController.h"
#import "AppDelegate.h"
@interface CDGuideViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionFlow;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;

@end

@implementation CDGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _collectionFlow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionFlow.minimumLineSpacing = 0;
    _collectionFlow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.scrollsToTop = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerNib:[UINib nibWithNibName:@"CDGuideCell" bundle:nil] forCellWithReuseIdentifier:@"kCDGuideCellID"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"返 回(2)"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0, 0, 60, 40)];
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -20,0, 0);
    [button addTarget:self action:@selector(navigationItemBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem =  item;
}

-(void)navigationItemBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 9;
}


- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CDGuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kCDGuideCellID" forIndexPath:indexPath];
    cell.gImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"g%ld",indexPath.row+1]];
    return cell;
}

#pragma mark - UICollectionViewDelegateLeftAlignedLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake(kDeviceWidth, kDeviceHeight);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
  
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //得到一个当前的页数
    //滚动视图的偏移量 （持有的数据） 屏幕的宽（持有的数据）

    //滚动视图X的偏移量
    CGFloat x= scrollView.contentOffset.x;
    //屏幕的宽
    CGFloat w= CGRectGetWidth(self.view.frame);
    NSLog(@"%f",x);
    //偏移量除以宽 得到当前页面的页数
    NSInteger curPage = x/w;

//    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:119];
    //设置pageControl的当前页面
    self.pageControl.currentPage =curPage;
    
    if (curPage == 8) {
        [self.doneBtn setHidden:NO];
    }else{
        [self.doneBtn setHidden:YES];
    }
}
- (IBAction)doneClick:(id)sender {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"done"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    CDHomeViewController *vc = [[CDHomeViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [UIApplication sharedApplication].delegate.window.rootViewController = nav;
    
}

@end
